package com.zhuchaooasystem.presenter.login;

import android.app.Activity;
import android.os.Build;
import android.text.TextUtils;

import com.socks.library.KLog;
import com.zhuchaooasystem.api.HttpFactory;
import com.zhuchaooasystem.api.HttpUtil;
import com.zhuchaooasystem.api.RxResultHelper;
import com.zhuchaooasystem.base.BasePresenter;
import com.zhuchaooasystem.base.SchedulersCompat;
import com.zhuchaooasystem.entity.UserEntity;
import com.zhuchaooasystem.entity.UserInfo;
import com.zhuchaooasystem.ui.home.HomeActivity;
import com.zhuchaooasystem.utils.AppUtils;
import com.zhuchaooasystem.utils.EncryptUtils;
import com.zhuchaooasystem.utils.SharedprefrenceUtils;
import com.zhuchaooasystem.utils.ToastUtil;
import com.zhuchaooasystem.widget.OkDialog;

import java.util.Map;

import cn.jpush.android.api.JPushInterface;
import rx.Subscription;
import rx.functions.Action1;

/**
 * @author yinsi
 */

public class LoginPresenter extends BasePresenter<LoginContract.View> implements LoginContract.Presenter {

    public LoginPresenter(Activity activity, LoginContract.View view) {
        super(activity, view);
    }

    @Override
    public void login() {
        String mobile = mView.getMobile();
        final String password = mView.getPassword();
        //----
        if (TextUtils.isEmpty(mobile)) {
            handleError("账号不能为空");
            return;
        }
        if (!(!TextUtils.isEmpty(password) && password.length() >= 6)) {
            handleError("密码不得低于6位");
            return;
        }
        showLoading();
        mView.setLoginClick(false);
        //----
        final Map<String, String> map = HttpUtil.getHttpMap(mActivity);
        map.put("LoginName", mobile);
        map.put("Passcode", EncryptUtils.encryptPassword(password));
        map.put("type", EncryptUtils.getuniqueId(mActivity));
        map.put("Alias", Build.MODEL);
        //----
        Subscription subscription = HttpFactory.getHttpApi().loginApi(map)
                .compose(SchedulersCompat.applyIoSchedulers())
                .compose(RxResultHelper.handleResult()).subscribe(new Action1<Object>() {
                    @Override
                    public void call(Object o) {
                        //----
                        LoadingDiaogDismiss();
                        mView.setLoginClick(true);
                        if (null != o) {
                            UserEntity entity = (UserEntity) o;
                            if (null != entity.getResult().getSuccessful()) {
                                UserInfo.saveLoginInfo(entity.getResult().getSuccessful().getMemberId(), entity.getResult().getSuccessful().getUserName(), password,
                                        entity.getResult().getSuccessful().getUserPhone(), entity.getResult().getSuccessful().getUserPostion(), entity.getResult().getSuccessful().getRoleEmployee()
                                        , entity.getResult().getSuccessful().getUserDeparet());
                                ToastUtil.showShort(mActivity, "登录成功");
                                SharedprefrenceUtils.setUsername(mActivity, entity.getResult().getSuccessful().getUserName());
                                AppUtils.jump(mActivity, HomeActivity.class);
                                mActivity.finish();
                            } else if (null != entity.getResult().getError()) {
                                if (entity.getResult().getError().equals("用户已经离职")) {
                                    JPushInterface.stopPush(mActivity);
                                    //----清空登录记录
                                    UserInfo.getInstance().clearLoginInfo();
                                    //----
                                    OkDialog dialogOk = new OkDialog(mActivity);
                                    dialogOk.show();
                                    dialogOk.setTextContent("您已经离职");
                                    dialogOk.setOnOkClickListener(new OkDialog.OnOkClickListener() {
                                        @Override
                                        public void onClick(OkDialog dialog) {
                                            dialog.dismiss();
                                        }
                                    });
                                } else {
                                    handleError(entity.getResult().getError());
                                }
                            }
                        } else {
                            handleError("登录失败");
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        LoadingDiaogDismiss();
                        mView.setLoginClick(true);
                        handleError(throwable);

                        KLog.e("tag", throwable.toString());
                    }
                });
        addSubscrebe(subscription);
    }
}
